/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.find.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.find.entity.CourseAnalysis;
import com.thinkgem.jeesite.modules.find.service.CourseAnalysisService;

/**
 * 考点解析Controller
 * @author 罗洪州
 * @version 2018-02-08
 */
@Controller
@RequestMapping(value = "${adminPath}/find/courseAnalysis")
public class CourseAnalysisController extends BaseController {

	@Autowired
	private CourseAnalysisService courseAnalysisService;
	
	@ModelAttribute
	public CourseAnalysis get(@RequestParam(required=false) String id) {
		CourseAnalysis entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = courseAnalysisService.get(id);
		}
		if (entity == null){
			entity = new CourseAnalysis();
		}
		return entity;
	}
	
	@RequiresPermissions("find:courseAnalysis:view")
	@RequestMapping(value = {"list", ""})
	public String list(CourseAnalysis courseAnalysis, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CourseAnalysis> page = courseAnalysisService.findPage(new Page<CourseAnalysis>(request, response), courseAnalysis); 
		model.addAttribute("page", page);
		return "modules/find/courseAnalysisList";
	}

	@RequiresPermissions("find:courseAnalysis:view")
	@RequestMapping(value = "form")
	public String form(CourseAnalysis courseAnalysis, Model model) {
		model.addAttribute("courseAnalysis", courseAnalysis);
		return "modules/find/courseAnalysisForm";
	}

	@RequiresPermissions("find:courseAnalysis:edit")
	@RequestMapping(value = "save")
	public String save(CourseAnalysis courseAnalysis, Model model, RedirectAttributes redirectAttributes) {
        courseAnalysis.setContent(StringEscapeUtils.unescapeHtml4(courseAnalysis.getContent().trim()));

        if(!(StringUtils.isEmpty(courseAnalysis.getImg()) || courseAnalysis.getImg().contains("http://"))) {
            courseAnalysis.setImg( Global.CURRENT_SERVER+courseAnalysis.getImg() );
        }
        if (!beanValidator(model, courseAnalysis)){
			return form(courseAnalysis, model);
		}
		courseAnalysisService.save(courseAnalysis);
		addMessage(redirectAttributes, "保存analysis成功");
		return "redirect:"+Global.getAdminPath()+"/find/courseAnalysis/?repage";
	}
	
	@RequiresPermissions("find:courseAnalysis:edit")
	@RequestMapping(value = "delete")
	public String delete(CourseAnalysis courseAnalysis, RedirectAttributes redirectAttributes) {
		courseAnalysisService.delete(courseAnalysis);
		addMessage(redirectAttributes, "删除analysis成功");
		return "redirect:"+Global.getAdminPath()+"/find/courseAnalysis/?repage";
	}

}